
define([
	'vue',
	'config',
	'common',
	'../model/upLoadImg'
	],function(Vue,config,utils,upLoadImg){
	'use strict';

	var remittanceVoucherView=new Vue({
		el:'#app',
		data:{
			transferVoucher:'',
			id:'',
			status:'',
			rangliMoney:'',
			type:'', 			//是否重新提交 1：首次提交，2:重新提交，默认为首次提交
			onprogress:'',
			onprogressAgain:'',
			transferUserName:'',
			transferMoney:'',
			fileList:{
				transferVoucher:'',
			},
			submitBtn:false
			
		},
		created:function(){
			utils.userToken();
			//utils.pushHistory();

			// window.addEventListener('popstate',function() {
	  //      		utils.appAlert('是否放弃本次操作',function(){
	  //      			utils.pushHistory();
	  //      		},'取消',function(){
			// 		window.location.href = "expensesRecord_shop.html";
			// 	},'确定')
		 //     })

			var _this = this;
			_this.id = utils.getQueryVariable('id');
			if(!_this.id){
				history.back();
			}
			utils.ajax(utils.getWebUrl('declaration/get'),{
				id: _this.id
			},'POST',function(res){
				//console.log(res)
				var status = res.data.status;

				if(status === '5'){
					history.go(-2);
				}

				switch (status) {
					case '1':  //首次提交
						_this.type = 1;
						break;
					case '3':   //重新提交
						_this.type = 2;
						break;
					default:
						_this.type = 1;	
						break;
				}

				_this.rangliMoney = res.data.rangliMoney;
			},function(){
				$('.DataLoading').show();
			},function(){
				$('.DataLoading').hide();
			})
		},
		methods:{
			back:function(){
				utils.appAlert('是否放弃本次操作',function(){
	       			utils.pushHistory();
	       		},'取消',function(){
					window.location.href = "expensesRecord_shop.html";
				},'确定')
			},
			//第一次上传
			selectPhoto:function(event){
				var _this = this;
				var id = event.currentTarget.id;
				var $target = $(event.currentTarget);
				var $targetPic = $(event.currentTarget).parents('.Again').find('.imgAgain');

				$targetPic.show();
				$target.parents('.report-file').hide();

				var pic = upLoadImg.showPic($targetPic,$target);

				// 选择完立即压缩
				this.getFiles(id);
			},
			//再次上传
			selectAgain:function(event){
				var $target = $(event.currentTarget);
				var $targetPic = $target.parent('.imgAgain');
				var pic = upLoadImg.showPic($targetPic,$target);
				$target.parents('.Again').find('.file-prew').addClass('remove');

				var id = $target.parents('.Again').find('.file-prew').attr("id");
				// 选择完立即压缩
				this.getFiles(id);
			},
			getFiles:function(ele){
				var files;
				var _this = this;
				var element = $('#'+ele);
				if(element.hasClass('remove')){
					files = element.parents('.Again').find('.inputAgain').get(0).files[0];
				}else{
					files = element.get(0).files[0];
				}
				
				if(!files){
					_this.fileList[ele] = '';
				}else{
					lrz(files,{width:640})
					.then(function (rst) {
			        _this.fileList[ele] = rst.base64;
			        })
			        .catch(function (err) {
			        _this.fileList[ele] = '';
			        })
			        .always(function () {
			        });
				}

			},
			save:function(){

				var _this = this;

				var getPic = function(element,isLoading){
					var pic;
					if(isLoading === "t"){
						pic = $(element).find('.pic').attr('src');
					}else{
						pic = $(element).find('.pic');
					}
					return pic;
				}

				// var getFiles = function(element){
				// 	var files;
				// 	var element = $(element);
				// 	if(element.hasClass('remove')){
				// 		return files = element.parents('.Again').find('.inputAgain').get(0).files[0];
				// 	}else{
				// 		return files = element.get(0).files[0];
				// 	}
				// }

				if($.trim(_this.transferUserName) === ""){
					utils.appAlert('转款人不能为空');
                    return;
				}

				if($.trim(_this.transferMoney) === ""){
					utils.appAlert('款转金额不能为空');
                    return;
				}

				if(_this.transferMoney<0){
					utils.appAlert('款转金额不能为负数');
                    return;
				}

				if(!(/^(0|[1-9]\d*)(\.\d{1,2})?$/.test(_this.transferMoney))){
					utils.appAlert('请输入正确的金额');
                    return;
				}

				if($.trim(getPic('#transferVoucherPic','t')) === ""){
					utils.appAlert('请上传转款凭据');
                    return;
				}

				if(_this.fileList.transferVoucher == ''){
					utils.appAlert('图片处理中，请稍后...');
					return;
				}

				if (_this.submitBtn) {
					return;
				}

				_this.submitBtn = true;

				// var filesList = {
				// 	businessLicense: getFiles('#transferVoucher')
				// }

				var onprogress = function(evt){
					var loaded = evt.loaded;     //已经上传大小情况 
					var tot = evt.total;      	 //附件总大小 
					var per = Math.floor(100*loaded/tot);  //已经上传的百分比
				    $('.upLoadimg').css({'height':(100-per)+'%'});
				}

				var formData = new FormData();
				$.each(_this.fileList,function(k,p){
					if(p){
						formData.append("imgStr",p);
					}
				})

				upLoadImg.ajax(formData,onprogress,function(res){
					var urls = res.urls;
					getPic('#transferVoucherPic').attr('src',urls[0]);

					_this.isFirstapply = true;
					utils.ajax(utils.getWebUrl('declaration/update'),{
						id:_this.id,
						transferVoucher: urls[0],
						type: _this.type,
						transferUserName: _this.transferUserName,
						transferMoney: _this.transferMoney
					},'POST',function(res){
						window.location.href="consumerAudit.html?id="+_this.id;
					},null,null,null,function(res){
						_this.submitBtn = false;
						utils.appAlert(res.msg);
					})
				},null,null,null,function(res){
					_this.submitBtn = false;
					utils.appAlert(res.msg);
				})

				

				// var format = upLoadImg.uploadFile(filesList);
				// upLoadImg.ajax(format,onprogress,function(res){
				// 	var urls = res.urls;
				// 	getPic('#transferVoucherPic').attr('src',urls[0]);

				// 	utils.ajax(utils.getWebUrl('declaration/update'),{
				// 		id:_this.id,
				// 		transferVoucher: urls[0],
				// 		type: _this.type,
				// 		transferUserName: _this.transferUserName,
				// 		transferMoney: _this.transferMoney
				// 	},'POST',function(res){
				// 		window.location.href="consumerAudit.html?id="+_this.id;
				// 	})
				// })
			}
		}
	})

	return remittanceVoucherView;
});